/**
 * 文章置顶/取消置顶
 */

var router = require('express').Router();
var assertAlljs = require('../bin/util/assert-alljs');
var userTool = require('../bin/user');
var articleMarkTool = require('../bin/articleMark');

router.post('/articlepromote', async function(req, res) {
    let user = req.session.user;
    let assert = assertAlljs(req, res);
    let checkMessage = userTool.check(user, {role: ['admin', 'admin:'+res.locals.blockRoot]});
    if (await assert(checkMessage))
        return;

    let type = req.body.type;

    let filter = {};
    if (await assert(req.body.id == null || req.body.id === ''))
        return;

    req.behavior.set(JSON.stringify({type, filter}));
    if (type === 'promote'){
        let result = articleMarkTool.promoteArticle(req.body.id);
        if (await assert(result instanceof Error))
            return;
        await assert(true, 'promote 1 records');
    }else if (type === 'demote'){
        let result = articleMarkTool.demoteArticle(req.body.id);
        if (await assert(result instanceof Error))
            return;
        await assert(true, 'demote 1 records');
    }else{
        await assert(true, 'unrecognize promote type');
    }
});
module.exports = router;